package com.sofar.gems.framework.vo;


import lombok.Getter;

/**
 * @author FENGZENG
 * @date 2021/8/20 10:55
 */
@Getter
public class R<T> {

    private Integer code;
    private String message;
    private T data;

    private R() {}

    public static<T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }

    public static<T> R<T> error(ResultCodeEnum resultCodeEnum) {
        R<T> r = new R<>();
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }

    public static <T> R<T> error(String message) {
        R<T> r = new R<>();
        r.setCode(ResultCodeEnum.ANOTHER_ERROR.getCode());
        r.setMessage(message);
        return r;
    }

    private void setCode(Integer code) {
        this.code = code;
    }

    private void setMessage(String message) {
        this.message = message;
    }

    private void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
